<?php 
class contacto extends Controller{
	
	  function __construct(){
	  	  parent::Controller();
	  	  $this->load->model(array('Conctato','usuarios','mcarpetastatus','mcompaniacarpetas','mtransaccionentrada'));
	  	 
	  }
	   
	  public function index(){
	  	        
	  	    $this->load->view('main', array('content_page' => 'listContacto','menutop' => 0));
	  	
	  }
	  
	  public function excel(){
	  	//$array = $this->Conctato->loadCompany();
	  	$data = array(
	  	              0=>array(
	  	                         'Nombre'=>'Natanael',
	  	                         'Apellido'=>'acosta',
	  	                         'Edad'=>25  
	  	                      ),
	  	              1=>array(
	  	                         'Nombre'=>'Cornelio',
	  	                         'Apellido'=>'Cabrar',
	  	                         'Edad'=>28  
	  	                      )
	  	             ); 
	  	//$arrayExcel = array('result_array'=>$data);
	  	$this->load->view('main', array('content_page' => 'listContacto','menutop' => 0));
	  }
	  
	  public function conpaniaArray(){  
	  	         $array = $this->Conctato->loadCompany();
	  	         $numeroArray = $array->num_rows();
                 $data = $array->result_object();
                 $dataOro = array();
                 $dataError = array();
 
                 for ($i = 0; $i < $numeroArray; $i++){

                     foreach ($data[$i] as $k=>$j){
                     	                           switch ($k){
                     	                           	  case 'NombreCompania':
                     	                           	  	$datai[0] = $j;
                     	                           	  break;
                     	                           	  case 'IdServicio':
                     	                           	  	$datai[1] = $j;
                     	                           	  break; 
                     	                           	  case 'FechaSysCompania':
                     	                           	  	$datai[2] = $j;
                     	                           	  break;
                     	                           	   case 'EmailCompania':
                     	                           	  	$datai[3] = strtolower($j);
                     	                           	  break;
                     	                           	  case 'idStatu':
                     	                           	  	$datai[4] = $j;
                     	                           	  break;
                     	                           	  case 'Contacto':
                     	                           	  	$datai[5] = $j;
                     	                           	  break;
                     	                           }
                     	                          
                   }
	                if ($datai[3] != 'info@company24h.com'){
	                	                   $error = FALSE;
	                	                   for($k = 0; $k < count($dataOro);$k++){
	                	                   	   if ($dataOro[$k][3] == $datai[3]) {
	                	                   	   	   $error = TRUE;
	                	                   	   }  
	                	                   }                          
	                	                   if ($error == FALSE){
	                	                   	    $dataOro[] = $datai; 
	                	                   }else{
	                	                   	    $dataError[] = $datai; 
	                	                   } 
	                	
         	                                                
                    }
                  
                    $datai = null;
	
           }
           return array(0=>$dataOro,1=>$dataError);
	  	    $this->excel($dataOro);
	  }
	  
	  public function update(){

	  	   $array = $this->inbaundArray();
	  	   
	  	   for ($i = 0; $i < count($array);$i++){
	  	   	     if (
	  	   	         $array[$i][4] == 'staff@company24h.com' || 
	  	   	         $array[$i][4] == 'support@company24h.com' ||
	  	   	         $array[$i][4] == 'adm@company24h.com' ||
	  	   	         $array[$i][4] == 'f.lebreault@codete.net.do' ||
	  	   	         $array[$i][4] == 'smailing@company24h.com' ||
	  	   	         $array[$i][4] == 'staff@copany24h.com' ||
	  	   	         $array[$i][4] == '' ||
	  	   	         $array[$i][4] == 'null' 
	  	   	         ) {
	  	   	         	   $arrayi = array(
	  	   	         	                  
	  	   	         	                  'email'=>'info@company24h.com' 
	  	   	         	                 );


	  	   	         	     if ($this->Conctato->upDate($arrayi,$array[$i][0]) == true) {
	  	   	         	     	  $retorno[] = $array[$i][0].'  =   TRUE';
	  	   	         	     }else{
	  	   	         	     	 $retorno[] = $array[$i][0].'  =   FALSE';
	  	   	         	     }
	  	   	         	                 
	  	   	     	      
	  	   	            }
	  	   	
	  	   	 
	  	   }
	  	   
           return $retorno;
	  	
	  }
	  
	  public function inbaundArray(){
	  	
	  	              $array = $this->Conctato->loadInbound();
	  	              $numeroArray = $array->num_rows();
                      
                      $data = $array->result_object();
                      $dataOro = array();
             
                      
                          for ($i = 0; $i < $numeroArray;$i++){
                                             foreach ($data[$i] as $k=>$j){ 
                                             	        switch ($k){
                     	                           	               case 'IdTransaccion':
                     	                           	  	                $datai[0] = $j;
                     	                           	               break;
                     	                           	               
                     	                           	               case 'email':
                     	                           	  	                $datai[3] = strtolower($j);
                     	                           	               break;

                     	                           	               case 'idMotivoSalida':
                     	                           	  	                $datai[4] = $j;
                     	                           	               break;
                                    	                           }
                                                               
                                             	 
	  	     	  
	                                                 }
	                        
                                                    if ($datai[3] != 'info@company24h.com'){
                                                    	   $erro = FALSE;
                                                    	    for ($k = 0; $k < count($dataOro); $k++){
                                                    	    	
                                                    	    	if ($dataOro[$k][3] == $datai[3]) {
                                                    	    		 break 1;
                                                    	    	}
                                                    	    }
                                                    	  
                                                    	    	$dataOro[] = $datai; 
                                                    	                                  	
                                                    	  
                                                    }
         	                                        
                                                 $datai = null;
	
                                                }
                                                
	  	                                   return $dataOro;
	  	
	  }
	  
	  public function dataClientesPotencial(){
	  	       
	           $arraySegimiento=  $this->conpaniaArray();
	  	       $arraySegimiento = $arraySegimiento[0];  
	  	       
	  	       $arrayInbaund =  $this->inbaundArray();
	  	       $arrayInbaund = $arrayInbaund;
                
	            for ($j = 0;$j < count($arrayInbaund); $j++){
                     if ($arrayInbaund[$j][4] != 6 && $arrayInbaund[$j][4] != 9 && $arrayInbaund[$j][4] != 10 && $arrayInbaund[$j][4] != 0) {
                     	$arrayInbaundReal[] = $arrayInbaund[$j];
                     }
	            	
                }
	  	       
                $total = array();
               
               for ($k = 0; $k < count($arrayInbaundReal); $k++){
               	
               	     for ($j = 0;$j < count($arraySegimiento);$j++){
                             if ($arrayInbaundReal[$k][3] == $arraySegimiento[$j][3]) {
                             	break 1;
                             }               	     	
               	     }
               	     
               	    $total[] =  $arrayInbaundReal[$k];
               	
               } 
                
	  	       
	  	       $dataArray = '';
	  	       foreach ($total as $k){ 
	  	       	
	  	       	   $dataArray[] = $k[0];
	  	       }
	  	       
	  	       return $dataArray;
	  	       
	  	       
	  }
	  
	  
	  public function data(){
	  	    
	  	 
		
			
		    $company = $this->mtransaccionentrada->LoadTransacionAdmin($this->dataClientesPotencial());
			
				
			 return $company;	
		     //return  json_encode($company);		
				
			}
			
	

		function data2() {
			$page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
			$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'ID_airport'; // Columna para el order by
			$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'asc'; // Orden (asc|desc)
			$limit = $this->input->post('rows') ? $this->input->post('rows') : 10;
			$start = $limit * $page - $limit;
			if($start < 0) $start = 0;

			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
			header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
			header("Cache-Control: no-cache, must-revalidate" );
			header("Pragma: no-cache" );

			$records = $this->mtransaccionentrada->LoadTransacionAdmin($this->dataClientesPotencial());

			$totalRecords = $this->mtransaccionentrada->GetCountAdmin($this->dataClientesPotencial());

			$totalPages = ceil($totalRecords/$limit);
			if ($page > $totalPages) $page = $totalPages;

			$json_output = array(
				'page'		=> $page,
				'total'		=> $totalPages,
				'records'	=> $totalRecords,
				'rows'		=> array()
			);

			if($records != false) {
				foreach($records as $estatus) {
				          
					 
					   
					      $arr = $json_output['rows'][] = array(
					                                            'IdTransaccion'=>$estatus->getIdTransaccion(), 	
					                                            'referencia' =>$estatus->getReferencia(), 
					                                            'email' => $estatus->getEmail(),
					                                            'DateSistema' => $estatus->getFechaH()
					                                            ); 
					                                            
				}
			}

			echo json_encode($json_output);
		}

function to_excel($query, $filename='exceloutput')
{
     $headers = ''; // just creating the var for field headers to append to below
     $data = ''; // just creating the var for field data to append to below
     
     $obj =& get_instance();
     
     $fields = $query->field_data();
     if ($query->num_rows() == 0) {
          echo '<p>The table appears to have no data.</p>';
     } else {
          foreach ($fields as $field) {
             $headers .= $field->name . "\t";
          }
     
          foreach ($query->result() as $row) {
               $line = '';
               foreach($row as $value) {                                            
                    if ((!isset($value)) OR ($value == "")) {
                         $value = "\t";
                    } else {
                         $value = str_replace('"', '""', $value);
                         $value = '"' . $value . '"' . "\t";
                    }
                    $line .= $value;
               }
               $data .= trim($line)."\n";
          }
          
          $data = str_replace("\r","",$data);
                         
          header("Content-type: application/x-msdownload");
          header("Content-Disposition: attachment; filename=$filename.xls");
          echo "$headers\n$data";  
     }
}
        


function to_excel2($array, $filename='exceloutput')
{
     $headers = ''; // just creating the var for field headers to append to below
     $data = ''; // just creating the var for field data to append to below
     
     $obj =& get_instance();
     
           if (count($array) == 0) {
                 echo '<p>The table appears to have no data.</p>';
            }else{
            	  foreach ($arry as $k){
            	  	echo $k.'<br/>';
            	  }
            	
            } 
    }
			
	  	
}
	  
	  
?>